//
//  BoozeProductVC.m
//  VeganIsEasy
//
//  Created by Matthew Feinberg on 2/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "BoozeProductVC.h"
#import "BoozeUtils.h"


@implementation BoozeProductVC

@synthesize generalInformation, product_status, booze_type, created_on, updated_on, display_name;

- (id)initWithProductId:(NSInteger)primaryKey database:(sqlite3 *)db {
	if (self = [super initWithStyle:UITableViewStyleGrouped]) {
		productId = primaryKey;
		database = db;
	}
	return self;
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];

	char *sql = "SELECT p.company_id, p.created_on, p.display_name, p.id, p.product_status, p.updated_on, c.booze_type FROM product p JOIN company c ON (p.company_id=c.id) WHERE p.id=?";
	sqlite3_stmt *statement = nil;
	self.generalInformation = [NSMutableArray array]; //newArray;
	
	if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
		sqlite3_bind_int(statement, 1, productId);
		if (sqlite3_step(statement) == SQLITE_ROW) {
			char* tmp;
			tmp = (char *)sqlite3_column_text(statement, 2);
			if (tmp != NULL) {
				self.display_name = [NSString stringWithUTF8String:tmp];
				self.navigationItem.title = self.display_name;
			}
			tmp = (char *)sqlite3_column_text(statement, 4);
			if (tmp != NULL) {
				self.product_status = [NSString stringWithUTF8String:tmp];
//				[generalInformation addObject:[NSArray arrayWithObjects:@"Status",[NSString stringWithUTF8String:tmp],nil]];
			}
			tmp = (char *)sqlite3_column_text(statement, 1);
			if (tmp != NULL) {
				NSString *dateString = [BoozeUtils formatDateString:[NSString stringWithUTF8String:tmp]];
				if (dateString != nil) {
					self.created_on = dateString;
					//[generalInformation addObject:[NSArray arrayWithObjects:@"Added",dateString,nil]];
				}
			}
			tmp = (char *)sqlite3_column_text(statement, 5);
			if (tmp != NULL) {
				NSString *dateString = [BoozeUtils formatDateString:[NSString stringWithUTF8String:tmp]];
				if (dateString != nil) {
					self.updated_on = dateString;
					//[generalInformation addObject:[NSArray arrayWithObjects:@"Updated",dateString,nil]];
				}
			}
			tmp = (char *)sqlite3_column_text(statement, 6);
			if (tmp != NULL) {
				self.booze_type = [NSString stringWithUTF8String:tmp];
			}
		}
	}
	sqlite3_finalize(statement);
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 2;
}

- (UIImage *)statusIcon {
	NSString *iconName = nil;
	if ([product_status isEqualToString:@"Vegan Friendly"]) {
		iconName = [NSString stringWithFormat:@"%@_vegan_icon_small.png",[booze_type lowercaseString]];
	}
	else {
		iconName = [NSString stringWithFormat:@"%@_not_vegan_icon_small.png",[booze_type lowercaseString]];
	}
	if (iconName != nil) {
		return [UIImage imageNamed:iconName];
	}
	return nil;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSInteger row = [indexPath row];
	NSInteger section = [indexPath section];
	UITableViewCell *cell;
	
	if (section == 0) {
		if (row == 0) {
			cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
			if (!cell) {
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"titleCell"] autorelease];
				[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
				UIView *transparentBackground = [[UIView alloc] initWithFrame:CGRectZero];
				transparentBackground.backgroundColor = [UIColor clearColor];
				cell.backgroundView = transparentBackground;
				[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
				[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
				[[cell textLabel] setText:display_name];
				[[cell detailTextLabel] setText:booze_type];
				
				[[cell detailTextLabel] setFont:[[cell.detailTextLabel font] fontWithSize:[UIFont smallSystemFontSize]]];
				
			}
			return cell;
			
		}
		else if (row == 1) {
			cell = [tableView dequeueReusableCellWithIdentifier:@"statusCell"];
			if (!cell) {
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"statusCell"] autorelease];
				[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
				UIView *transparentBackground = [[UIView alloc] initWithFrame:CGRectZero];
				transparentBackground.backgroundColor = [UIColor clearColor];
				cell.backgroundView = transparentBackground;
				[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
				//				[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
				[[cell imageView] setImage:[self statusIcon]];
				[[cell textLabel] setText:product_status];
				
				[[cell textLabel] setFont:[[cell.textLabel font] fontWithSize:[UIFont smallSystemFontSize]]];
				[[cell textLabel] setTextColor:[UIColor grayColor]];
				
			}
			return cell;
			
		}
	}

	cell = [tableView dequeueReusableCellWithIdentifier:@"generalInformationCell"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"generalInformationCell"] autorelease];
		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
	}
		
	if (row == 0) {
		[[cell textLabel] setText:@"Added"];
		[[cell detailTextLabel] setText:self.created_on];
	}
	else {
		[[cell textLabel] setText:@"Updated"];
		[[cell detailTextLabel] setText:self.updated_on];			
	}

	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

